package com.kld.yqyj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.kld.common.utils.StringUtils;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.kld.yqyj.domain.bo.TDzKfdyCurInforBo;
import com.kld.yqyj.domain.vo.TDzKfdyCurInforVo;
import com.kld.yqyj.domain.TDzKfdyCurInfor;
import com.kld.yqyj.mapper.TDzKfdyCurInforMapper;
import com.kld.yqyj.service.ITDzKfdyCurInforService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 开发单元基础信息Service业务层处理
 *
 * @author bw
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class TDzKfdyCurInforServiceImpl implements ITDzKfdyCurInforService {

    private final TDzKfdyCurInforMapper baseMapper;

    /**
     * 查询开发单元基础信息
     *
     * @param geoId 开发单元基础信息主键
     * @return 开发单元基础信息
     */
    @Override
    public TDzKfdyCurInforVo queryById(String geoId){
        return baseMapper.selectVoById(geoId);
    }

    /**
     * 查询开发单元基础信息列表
     *
     * @param bo 开发单元基础信息
     * @return 开发单元基础信息
     */
    @Override
    public TableDataInfo<TDzKfdyCurInforVo> queryPageList(TDzKfdyCurInforBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TDzKfdyCurInfor> lqw = buildQueryWrapper(bo);
        Page<TDzKfdyCurInforVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询开发单元基础信息列表
     *
     * @param bo 开发单元基础信息
     * @return 开发单元基础信息
     */
    @Override
    public List<TDzKfdyCurInforVo> queryList(TDzKfdyCurInforBo bo) {
        LambdaQueryWrapper<TDzKfdyCurInfor> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TDzKfdyCurInfor> buildQueryWrapper(TDzKfdyCurInforBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TDzKfdyCurInfor> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getGeoName()), TDzKfdyCurInfor::getGeoName, bo.getGeoName());
        lqw.eq(StringUtils.isNotBlank(bo.getOcId()), TDzKfdyCurInfor::getOcId, bo.getOcId());
        lqw.like(StringUtils.isNotBlank(bo.getOcName()), TDzKfdyCurInfor::getOcName, bo.getOcName());
        lqw.eq(StringUtils.isNotBlank(bo.getOfId()), TDzKfdyCurInfor::getOfId, bo.getOfId());
        lqw.like(StringUtils.isNotBlank(bo.getOfName()), TDzKfdyCurInfor::getOfName, bo.getOfName());
        lqw.eq(StringUtils.isNotBlank(bo.getCYC()), TDzKfdyCurInfor::getCYC, bo.getCYC());
        lqw.eq(StringUtils.isNotBlank(bo.getGS()), TDzKfdyCurInfor::getGS, bo.getGS());
        return lqw;
    }

    /**
     * 新增开发单元基础信息
     *
     * @param bo 开发单元基础信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(TDzKfdyCurInforBo bo) {
        TDzKfdyCurInfor add = BeanUtil.toBean(bo, TDzKfdyCurInfor.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGeoId(add.getGeoId());
        }
        return flag;
    }

    /**
     * 修改开发单元基础信息
     *
     * @param bo 开发单元基础信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(TDzKfdyCurInforBo bo) {
        TDzKfdyCurInfor update = BeanUtil.toBean(bo, TDzKfdyCurInfor.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TDzKfdyCurInfor entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除开发单元基础信息
     *
     * @param geoIds 需要删除的开发单元基础信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
